/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2016
 */

package com.jzwl.xydk.manager.user.userBasicinfo.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jzwl.common.page.PageObject;
import com.jzwl.system.base.controller.BaseController;
import com.jzwl.xydk.manager.user.userBasicinfo.service.UserBasicinfoService;
import com.jzwl.xydk.wap.skillUser.service.SkillUserService;

/**
 * userBasicinfo
 * <p>Title:UserBasicinfoController </p>
 * 	<p>Description:UserBasicinfo </p>
 * 	<p>Company: </p> 
 * 	@author aotu-code 
 */
@Controller("userBasicinfo")
@RequestMapping("/userBasicinfo")
public class UserBasicinfoController extends BaseController {
	
	@Autowired
	private UserBasicinfoService userBasicinfoService;
	@Autowired
	private SkillUserService skillUserService;
	
	
	/**
	 * 跳转到添加UserBasicinfo
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/toAddUserBasicinfo")
	public String toAddUserBasicinfo(HttpServletRequest request, HttpServletResponse response,Model model) {
		
		//获取所有的城市，从xiaoka中进行获取
		List<Map<String, Object>> cityList = userBasicinfoService.getCityInfo();
		
		//获取省列表
		List<Map<String, Object>> provnceList = skillUserService.queryProviceListVersion2();
		
		
		model.addAttribute("url", "userBasicinfo/addUserBasicinfo.html");
		model.addAttribute("cityList", cityList);
		model.addAttribute("provnceList", provnceList);//省市
		
		return "/manager/user/add";
		
	}
	/**
	 * 添加UserBasicinfo
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/addUserBasicinfo")
	public String addUserBasicinfo(HttpServletRequest request, HttpServletResponse response,Model model) {
		
		try{
			createParameterMap(request);
			String userName = String.valueOf(request.getSession().getAttribute("userName"));
			if(!"".equals(userName)){
				paramsMap.put("createUser", userName);
			}else{
				paramsMap.put("createUser", "admin");
			}
			boolean flag = userBasicinfoService.addUserBasicinfo(paramsMap);
			if(flag){
				model.addAttribute("msg","添加成功");
				return "redirect:/userBasicinfo/userBasicinfoList.html?start=0";
			}else{
				model.addAttribute("msg","添加失败");
				return "/error";
			}
		}catch(Exception e){
			e.printStackTrace();
			model.addAttribute("msg","添加失败");
			return "/error";
		}
		
	}

	/**
	 * 跳转到修改页面
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/toUpdateUserBasicinfo")
	public String toUpdateUserBasicinfo(HttpServletRequest request, HttpServletResponse response,Model model) {
		
		try{
			createParameterMap(request);
			
			//获取省列表
			List<Map<String, Object>> provnceList = skillUserService.queryProviceListVersion2();
			model.addAttribute("provnceList", provnceList);
			//获取所有的城市，从xiaoka中进行获取
			/*List<Map<String, Object>> cityList = userBasicinfoService.getCityInfo();
			model.addAttribute("cityList", cityList);*/
			
			//List<Map<String, Object>> cityList = userBasicinfoService.queryCityByPro(proId);
			Map<String,Object> objMap = userBasicinfoService.getById(paramsMap);//详细信息
			model.addAttribute("objMap", objMap);
			String cityId = String.valueOf(objMap.get("cityId"));
			Map<String,Object> proMap = userBasicinfoService.getProvence(cityId);//查询所属省
			model.addAttribute("proMap", proMap);
			
			String province = String.valueOf(proMap.get("province"));
			List<Map<String, Object>>  cityList = userBasicinfoService.queryCityByPro(province);//根据省查出市
			model.addAttribute("cityList", cityList);
			
			String sholId = String.valueOf(objMap.get("schoolId"));
			Map<String,Object> sholMap = userBasicinfoService.getSholName(sholId);//查询所属学校
			model.addAttribute("sholMap", sholMap);
			model.addAttribute("url", "userBasicinfo/updateUserBasicinfo.html");
			return "/manager/user/edit";
			
		}catch(Exception e){
			e.printStackTrace();
			model.addAttribute("msg","获取数据失败");
			return "/error";
		}
		
	}
	/**
	 * 修改userBasicinfo
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/updateUserBasicinfo")
	public String updateUserBasicinfo(HttpServletRequest request, HttpServletResponse response,Model model) {
		
		try{
			createParameterMap(request);
			
			boolean flag = userBasicinfoService.updateUserBasicinfo(paramsMap);
			
			if(flag){
				return "redirect:/userBasicinfo/userBasicinfoList.html?start=0";
			}else{
				model.addAttribute("msg","保存失败");
				return "/error";
			}
		}catch(Exception e){
		
			e.printStackTrace();
			model.addAttribute("msg","保存失败");
			return "/error";
		}
	}
	
	/**
	 * userBasicinfolist
	 */
	@RequestMapping(value = "/userBasicinfoList")
	public String userBasicinfoList(HttpServletRequest request, HttpServletResponse response,Model model) {
		
		try{
			createParameterMap(request);
			PageObject po = new PageObject();
			po = userBasicinfoService.queryUserBasicinfoList(paramsMap);
			model.addAttribute("po", po);
			model.addAttribute("paramsMap",paramsMap);
			return "/manager/user/list";
		}catch(Exception e){
			e.printStackTrace();
			model.addAttribute("msg","获取数据失败");
			return "/error";
		}
		
	}

	/**
	 * 删除userBasicinfo
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/deleteUserBasicinfo")
	public String deleteUserBasicinfo(HttpServletRequest request, HttpServletResponse response,Model model) {
		
		try{
			createParameterMap(request);

			boolean flag = userBasicinfoService.deleteUserBasicinfo(paramsMap);
			
			if(flag){
				return "redirect:/userBasicinfo/userBasicinfoList.html?start=0";
			}else{
				model.addAttribute("msg","删除失败");
				return "/error";
			}
		}catch(Exception e){
			e.printStackTrace();
			model.addAttribute("msg","删除失败");
			return "/error";
		}
		
	}
	
	
	/**
	 * 根据城市获取城市下的学校
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/querySchoolInfoByCityId")
	public @ResponseBody Map querySchoolInfoByCityId(HttpServletRequest request, HttpServletResponse response,Model model) {
		createParameterMap(request);
		Map map  = new HashMap();
		List list = new ArrayList();
		list=userBasicinfoService.querySchoolInfoByCityId(paramsMap);
		map.put("schools",list);
		return map;
	}
	
	
	/**
	 * 根据省查出市
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/queryCityByPro")
	public @ResponseBody Map queryCityByPro(HttpServletRequest request, HttpServletResponse response,Model model) {
		createParameterMap(request);
		String proId = String.valueOf(paramsMap.get("proId"));
		Map map  = new HashMap();
		List list = new ArrayList();
		list=userBasicinfoService.queryCityByPro(proId);
		map.put("cityList",list);
		return map;
	}
	
	/**
	 * 根据市查出院校类别
	 * @return  list
	 * dxf
	 */
	@RequestMapping(value = "/queryScholTypeByCity")
	public @ResponseBody Map queryScholTypeByCity(HttpServletRequest request, HttpServletResponse response,Model model) {
		createParameterMap(request);
		String cityId = String.valueOf(paramsMap.get("cityId"));
		List list = new ArrayList();
		list = skillUserService.querySchollTypeVersion2(cityId);
		Map map  = new HashMap();
	
		map.put("scholTypeList",list);
		return map;
	}
	
	/**
	 * 根据院校类别查出学校
	 * @return  list
	 * dxf
	 */
	@RequestMapping(value = "/queryScholByType")
	public @ResponseBody Map queryScholByType(HttpServletRequest request, HttpServletResponse response,Model model) {
		createParameterMap(request);
		String cityId = String.valueOf(paramsMap.get("cityId"));
		String dcdaId = String.valueOf(paramsMap.get("dcdaId"));
		List list = new ArrayList();
		list = skillUserService.querySchollByTypeVersion2(cityId, dcdaId);
		Map map  = new HashMap();
	
		map.put("scholList",list);
		return map;
	}
	
	
}

